<?php
/**
 * 发货单请求
 *
 * @category 
 * @package 
 * @author chenping<chenping@shopex.cn>
 * @version $Id: Z
 */
class ome_event_trigger_shop_delivery
{
    /**
     * 通过发货单信息回打发货状态(发货调用)
     *
     * @return void
     * @author 
     **/
    public function delivery_confirm_send($delivery_id)
    {
        $deliveryModel = app::get('ome')->model('delivery');
        $delivery = $deliveryModel->getFinishDelivery($delivery_id);
        if ($delivery['process'] != 'true') return;

        
        if ($delivery['is_bind'] == 'true') { //  合单
            $children_delivery_id = array();

            $childrednDeliveryList = $deliveryModel->getList('*',array('parent_id'=>$delivery['delivery_id']));
            foreach ($childrednDeliveryList as $c_key => $c_delivery) {
                $childrednDeliveryList[$c_key]['status']               = $delivery['status'];
                $childrednDeliveryList[$c_key]['logi_id']              = $delivery['logi_id'];
                $childrednDeliveryList[$c_key]['logi_name']            = $delivery['logi_name'];
                $childrednDeliveryList[$c_key]['logi_no']              = $delivery['logi_no'];
                $childrednDeliveryList[$c_key]['delivery_cost_actual'] = $delivery['delivery_cost_actual'];

                $children_delivery_id[] = $c_delivery['delivery_id'];
            }

            // 发货单对应的订单
            $delivery_orders = array();

            $deliveryOrderModel = app::get('ome')->model('delivery_order');
            $deliveryOrderList = $deliveryOrderModel->getList('*',array('delivery_id'=>$children_delivery_id));
            foreach ($deliveryOrderList as $key => $value) {
                $delivery_orders[$value['delivery_id']] = &$orderList[$value['order_id']];
            }

            $orderModel = app::get('ome')->model('orders');
            $rows = $orderModel->getList('*',array('order_id'=>array_keys($orderList)));
            foreach ($rows as $key => $value) {
                $orderList[$value['order_id']] = $value;
            }

            foreach ($childrednDeliveryList as $c_delivery) {
                $this->_process_retry($c_delivery,$childrednDeliveryList,$delivery_orders);
            }

        } else {
            // 发货单对应的订单
            $delivery_orders = array();
            $delivery_orders[$delivery['delivery_id']] = $delivery['orders'][0];

            $this->_process_retry($delivery,array($delivery),$delivery_orders);
        }

        return true;
    }

    /**
     * 通过发货单ID回打发货状态(回打失败重试)
     *
     * @param Array $orderids
     * @return void
     * @author 
     **/
    public function delivery_confirm_retry($orderids)
    {
        if (!$orderids) return false;

        $orderModel = app::get('ome')->model('orders');
        $rows = $orderModel->getList('*',array('order_id'=>$orderids));
        $orderList = array();
        foreach ($rows as $row) {
            $orderList[$row['order_id']] = $row;
        }
        $deliveryOrderModel = app::get('ome')->model('delivery_order');
        $deliveryOrderList = $deliveryOrderModel->getList('*',array('order_id'=>$orderids));

        if (!$deliveryOrderList) return false;

        $delivery_orders = array();
        foreach ($deliveryOrderList as $key => $value) {
            $delivery_orders[$value['delivery_id']] = $orderList[$value['order_id']];
        }

        $deliveryModel = app::get('ome')->model('delivery');
        $deliveryList = $deliveryModel->getList('*',array('delivery_id'=>array_keys($delivery_orders),'process'=>'true','parent_id'=>'0'));
        foreach ($deliveryList as $delivery) {
            kernel::single('ome_event_trigger_logistics_electron')->delivery($delivery['delivery_id']);
            if ($delivery['is_bind'] == 'true') {
                $childrednDeliveryList = $deliveryModel->getList('*',array('parent_id'=>$delivery['delivery_id']));

                foreach ($childrednDeliveryList as $c_key => $c_delivery) {
                    $childrednDeliveryList[$c_key]['status']               = $delivery['status'];
                    $childrednDeliveryList[$c_key]['logi_id']              = $delivery['logi_id'];
                    $childrednDeliveryList[$c_key]['logi_name']            = $delivery['logi_name'];
                    $childrednDeliveryList[$c_key]['logi_no']              = $delivery['logi_no'];
                    $childrednDeliveryList[$c_key]['delivery_cost_actual'] = $delivery['delivery_cost_actual'];
                }

                foreach ($childrednDeliveryList as $c_delivery) {
                    $this->_process_retry($c_delivery,$childrednDeliveryList,$delivery_orders,true);
                }

            } else {
                $this->_process_retry($delivery,$deliveryList,$delivery_orders,true);
            }
        }

        return true;
    }

    private function _process_retry($curDelivery,$deliveryList,$delivery_orders,$retry=false)
    {
        $sdf = kernel::single('ome_event_trigger_shop_data_delivery_router')
                    ->set_shop_id($curDelivery['shop_id'])
                    ->init($deliveryList,$delivery_orders)
                    ->get_sdf($curDelivery['delivery_id']);
        if (!$sdf) return;

        if ($retry) kernel::single('erpapi_router_request')->set('shop',$curDelivery['shop_id'])->delivery_add($sdf);
        kernel::single('erpapi_router_request')->set('shop',$curDelivery['shop_id'])->delivery_logistics_update($sdf);
        kernel::single('erpapi_router_request')->set('shop',$curDelivery['shop_id'])->delivery_confirm($sdf);
    }


    /**
     *  发货单状态变更
     *
     * @return void
     * @author 
     **/
    public function delivery_process_update($deliveryids)
    {   
        $deliveryModel = app::get('ome')->model('delivery');
        $rows = $deliveryModel->getList('shop_id,delivery_bn,delivery_id,status,is_bind,logi_id',array('delivery_id'=>$deliveryids));

        if (!$rows) return;
        $deliveryList = array();
        
        foreach ($rows as $row) {
            $deliveryList[$row['delivery_id']] = $row;
           
        }


        $deliveryOrderModel = app::get('ome')->model('delivery_order');
        $rows = $deliveryOrderModel->getList('*',array('delivery_id'=>$deliveryids));
        if (!$rows) return;

        $delivery_order = array();
        foreach ($rows as $row) {
            $delivery_order[$row['order_id']] = $row['delivery_id'];
        }

        $orderModel = app::get('ome')->model('orders');
        $rows = $orderModel->getList('order_id,order_bn,shop_id',array('order_id'=>array_keys($delivery_order)));
        if (!$rows) return;

        $orderList = array();
        foreach ($rows as $row) {
            $orderList[$row['order_id']] = $row;
        }
        
        foreach ($delivery_order as $order_id => $delivery_id) {
            $delivery = $deliveryList[$delivery_id]; $order = $orderList[$order_id];

            if ($delivery['is_bind'] == 'true') {
                $c_delivery_order = $deliveryOrderModel->getList('*',array('order_id'=>$order_id));
                $c_deliveryIds = array();
                foreach ($c_delivery_order as $k=>$v) {
                    $c_deliveryIds[] = $v['delivery_id'];
                }

                $childDelivery = $deliveryModel->getList('delivery_id,delivery_bn,shop_id',array('delivery_id'=>$c_deliveryIds, 'parent_id'=>$delivery_id),0,1);
  
                $sdf = array(
                    'delivery_bn' => $childDelivery[0]['delivery_bn'],
                    'status'      => $delivery['status'],
                    'orderinfo'   => array('order_bn'=>$order['order_bn'])
                );

                kernel::single('erpapi_router_request')->set('shop',$childDelivery[0]['shop_id'])->delivery_deliveryprocess_update($sdf);

            } else {
                $sdf = array(
                    'delivery_bn' => $delivery['delivery_bn'],
                    'status'      => $delivery['status'],
                    'orderinfo'   => array('order_bn'=>$order['order_bn'])
                );

                kernel::single('erpapi_router_request')->set('shop',$delivery['shop_id'])->delivery_deliveryprocess_update($sdf);
            }
        }
    }

    /**
     * 发货单添加
     *
     * @return void
     * @author 
     **/
    public function delivery_add($delivery_id)
    {
        $deliveryModel = app::get('ome')->model('delivery');
        $deliveryList = $deliveryModel->getList('*',array('delivery_id'=>$delivery_id));

        if (!$deliveryList) return;
        $delivery = $deliveryList[0];

        // 发货单对应的订单
        $deliveryOrderModel = app::get('ome')->model('delivery_order');
        $deliveryOrderList = $deliveryOrderModel->getList('delivery_id,order_id',array('delivery_id'=>$delivery_id));

        $order_ids = array(); $delivery_orders = array();
        foreach ($deliveryOrderList as $key => $value) {
            $order_ids[] = $value['order_id'];

            $delivery_orders[$value['delivery_id']] = &$orderList[$value['order_id']];
        }

        $orderModel = app::get('ome')->model('orders');
        $rows = $orderModel->getList('*',array('order_id'=>$order_ids));
        foreach ($rows as $key => $value) {
            $orderList[$value['order_id']] = $value;
        }
       
        $sdf = kernel::single('ome_event_trigger_shop_data_delivery_router')
                    ->set_shop_id($delivery['shop_id'])
                    ->init($deliveryList,$delivery_orders)
                    ->get_add_delivery_sdf($delivery['delivery_id']);
                    
        if (!$sdf) return;

        kernel::single('erpapi_router_request')->set('shop',$delivery['shop_id'])->delivery_add($sdf);
    }

    /**
     *  发货单状态变更
     *
     * @return void
     * @author 
     **/
    public function delivery_logistics_update($deliveryids)
    {   
        $deliveryModel = app::get('ome')->model('delivery');
        $rows = $deliveryModel->getList('shop_id,delivery_bn,delivery_id,status,is_bind,logi_id,logi_name,logi_no',array('delivery_id'=>$deliveryids));

        if (!$rows) return;
        $deliveryList = array();
        $corpIds = array();
        foreach ($rows as $row) {
            $deliveryList[$row['delivery_id']] = $row;
            $corpIds[] = $row['logi_id'];
        }

        $corpModel = app::get('ome')->model('dly_corp');
        $rows = $corpModel->getList('type,corp_id',array('corp_id'=>$corpIds));
        $corps = array();
        foreach ($rows as $row) {
            $corps[$row['corp_id']] = $row['type'];
        }
        unset($rows);
        $deliveryOrderModel = app::get('ome')->model('delivery_order');
        $rows = $deliveryOrderModel->getList('*',array('delivery_id'=>$deliveryids));
        if (!$rows) return;

        $delivery_order = array();
        foreach ($rows as $row) {
            $delivery_order[$row['order_id']] = $row['delivery_id'];
        }

        $orderModel = app::get('ome')->model('orders');
        $rows = $orderModel->getList('order_id,order_bn,shop_id',array('order_id'=>array_keys($delivery_order)));
        if (!$rows) return;

        $orderList = array();
        foreach ($rows as $row) {
            $orderList[$row['order_id']] = $row;
        }

        foreach ($delivery_order as $order_id => $delivery_id) {
            $delivery = $deliveryList[$delivery_id]; $order = $orderList[$order_id];

            if ($delivery['is_bind'] == 'true') {
                $c_delivery_order = $deliveryOrderModel->getList('*',array('order_id'=>$order_id));
                $c_deliveryIds = array();
                foreach ($c_delivery_order as $k=>$v) {
                    $c_deliveryIds[] = $v['delivery_id'];
                }

                $childDelivery = $deliveryModel->getList('delivery_id,delivery_bn,shop_id',array('delivery_id'=>$c_deliveryIds, 'parent_id'=>$delivery_id),0,1);
                    foreach ($childDelivery as $child){
                        $sdf = array(
                            'delivery_bn' => $child['delivery_bn'],
                            'orderinfo'   => array('order_bn'=>$order['order_bn']),
                            'logi_type'   => $corps[$delivery['logi_id']],
                            'logi_name'   => $delivery['logi_name'],
                            'logi_no'     => $delivery['logi_no'],
                        );

                    kernel::single('erpapi_router_request')->set('shop',$child[0]['shop_id'])->delivery_logistics_update($sdf);

                }
                
            } else {
                $sdf = array(
                    'delivery_bn' => $delivery['delivery_bn'],
                    'orderinfo'   => array('order_bn'=>$order['order_bn']),
                    'logi_type'   => $corps[$delivery['logi_id']],
                    'logi_name'   => $delivery['logi_name'],
                    'logi_no'     => $delivery['logi_no'],
                );

                kernel::single('erpapi_router_request')->set('shop',$delivery['shop_id'])->delivery_logistics_update($sdf);
            }
        }
    }
}
